package com.example.huajia.wx.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.example.huajia.wx.db.dao.DynamicDao;
import com.example.huajia.wx.db.pojo.CommentEntity;
import com.example.huajia.wx.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.example.huajia.wx.common.util.RedisKey.DYNAMIC_ALL_KEY;
import static com.example.huajia.wx.common.util.RedisKey.DYNAMIC_MINE_KEY;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    DynamicServiceImpl dynamicService;

    @Resource
    DynamicDao dynamicDao;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<CommentEntity> insertComment(String superId, CommentEntity comment) {
        String[] ids = superId.split("#");
        StringBuilder commentId = new StringBuilder(ids[0]);
        List<CommentEntity> List = dynamicService.getComments(ids[0]);
        List<CommentEntity> list = List;
        for (int i = 1; i <ids.length ; i++) {
            commentId.append("#").append(ids[i]);
            System.out.println(commentId.toString());
            CommentEntity entity = null;
            for (int j = 0; j < list.size(); j++) {
                if(list.get(j).getId().equals(commentId.toString())){
                    entity = list.get(j);
                    break;
                }
            }
            if(entity.getComments()==null){
                entity.setComments(new ArrayList<CommentEntity>());
            }
            list = entity.getComments();
        }
        comment.setId(superId+"#"+ IdWorker.getIdStr(CommentEntity.class));
        Date now = new Date();
        DateUtil.offset(now, DateField.HOUR, 8);
        comment.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
        comment.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
        list.add(comment);
        dynamicDao.updateComment(ids[0],List); // 刷新评论区
        String city = comment.getCity();
        String userId = comment.getPromulgator();
        String key1 = DYNAMIC_ALL_KEY + city + ":";
        String key2 = DYNAMIC_MINE_KEY + userId + ":";
        redisTemplate.expire(key1, 0, TimeUnit.SECONDS);
        redisTemplate.expire(key2, 0, TimeUnit.SECONDS);
        return List;
    }

    @Override
    public List<CommentEntity> deleteComment(String id) {
        String[] ids = id.split("#");
        StringBuilder commentId = new StringBuilder(ids[0]);
        List<CommentEntity> List = dynamicService.getComments(ids[0]);
        List<CommentEntity> list = List;
        for (int i = 1; i <ids.length-1 ; i++) {
            commentId.append("#").append(ids[i]);
            CommentEntity entity = null;
            for (int j = 0; j < list.size(); j++) {
                if(list.get(j).getId().equals(commentId.toString())){
                    entity = list.get(j);
                    break;
                }
            }
            if(entity.getComments()==null){
                entity.setComments(new ArrayList<CommentEntity>());
            }
            list = entity.getComments();
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                list.remove(i);
            }
        }
        dynamicDao.updateComment(ids[0],List); // 刷新评论区
        return List;
    }
}
